package com.ideaction.bimodule.server;

import com.ideaction.globalconst.SysteamConst;
import com.ideaction.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.codec.SerializationCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

@Service
@Slf4j
public class CommonBiServer {
    
    public static final String SYSTEM_BI_COMMON = "system:bi:common";
    
    private Map<String, Consumer<Map<String, String>>> map = new HashMap<>();
    
    @Autowired
    private MongoTemplate playerMongoTemplate;
    
    @Autowired
    private RedissonClient redissonClient;
    
    @Autowired
    private ShutdownContext shutdownContext;
    
    @Autowired
    private ActivityService activityService;
    
    //todo 服务名
    @Value("#{123}")
    private String serverName;
    
    public CommonBiServer() {
        //基本上bi做对于用户的修改只能服务器
        //关闭服务器
        this.map.put("closeServer", (Map<String, String> i) -> {
            shutdownContext.showdown();
        });
        //设置gm登录状态
        this.map.put("setGMLogin", (Map<String, String> i) -> {
            String str = i.get("serverName");
            if (str.contains(serverName) || "all".equals(str)) {
                String type = i.get("type");
                SysteamConst.ISGMLOGIN = Boolean.parseBoolean(type);
            }
        });
        
        //更新活动信息
        this.map.put("updateActives", (Map<String, String> i) -> {
            String str = i.get("serverName");
            if (str.contains(serverName) || "all".equals(str)) {
                activityService.updateActivityData();
            }
        });
        //禁止
        this.map.put("forbid", (Map<String, String> i) -> {
            String str = i.get("serverName");
            if (str.contains(serverName) || "all".equals(str)) {
            
            }
        });
        
        //发送邮件
        this.map.put("sendMail", (Map<String, String> i) -> {
            String str = i.get("serverName");
            if (str.contains(serverName) || "all".equals(str)) {
            
            }
        });
        
        //发送公告
        this.map.put("sendNotice", (Map<String, String> i) -> {
            String str = i.get("serverName");
            if (str.contains(serverName) || "all".equals(str)) {
            
            }
        });
        
    }
    
    public void start() {
        RTopic topic = redissonClient.getTopic(SYSTEM_BI_COMMON, new SerializationCodec());
        topic.addListener(Map.class, (x, y) -> {
            String consumerKey = (String) y.get("consumerKey");
            map.get(consumerKey).accept(y);
        });
        
    }
    
}
